import * as request from './dict-api';
import { DictSlice, callTypes } from './dict-slice';

const { actions } = DictSlice;

export const fetchDict = (queryParams: any) => (dispatch: any) => {
  dispatch(actions.startCall({ callType: callTypes.list }));
  return request
    .fetchDict(queryParams)
    .then((response: any) => {
      // const { items: entities, totalCount } = response.data;
      const { content: entities, totalElements: totalCount } = response.data;
      dispatch(actions.entitiesFetched({ totalCount, entities }));
    })
    .catch((error: any) => {
      error.clientMessage = '找不到数据';
      dispatch(actions.catchError({ error, callType: callTypes.list }));
    });
};

// export const createDict = (entity: any) => (dispatch: any) => {
//   dispatch(actions.startCall({ callType: callTypes.action }));
//   return request
//     .createDict(entity)
//     .then((response: any) => {
//       // const entity = response.data;
//       dispatch(actions.entityCreated({}));
//     })
//     .catch((error: any) => {
//       error.clientMessage = '创建数据失败';
//       dispatch(actions.catchError({ error, callType: callTypes.action }));
//     });
// };

// export const deleteDict = (key: any) => (dispatch: any) => {
//   dispatch(actions.startCall({ callType: callTypes.action }));
//   return request
//     .deleteDict(key)
//     .then((response: any) => {
//       dispatch(actions.entityDeleted({ key }));
//     })
//     .catch((error: any) => {
//       error.clientMessage = '删除数据失败';
//       dispatch(actions.catchError({ error, callType: callTypes.list }));
//     });
// };
// export const updateDict = (key: any, entity: any) => (dispatch: any) => {
//   dispatch(actions.startCall({ callType: callTypes.action }));
//   return request
//     .updateDict(key, entity)
//     .then((response: any) => {
//       // const entity = response.data;
//       dispatch(actions.entityUpdated({}));
//     })
//     .catch((error: any) => {
//       error.clientMessage = '更新数据失败';
//       dispatch(actions.catchError({ error, callType: callTypes.list }));
//     });
// };
